package com.liu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.reggie.common.R;
import com.liu.reggie.dto.DishDto;
import com.liu.reggie.pojo.Category;
import com.liu.reggie.pojo.Dish;
import com.liu.reggie.pojo.DishFlavor;
import com.liu.reggie.service.CategoryService;
import com.liu.reggie.service.DishFlavorService;
import com.liu.reggie.service.DishService;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.org.apache.xpath.internal.operations.Or;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author liustart
 * @create 2022-07-27 16:44
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping
    public  R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        String dishReids="dish_"+dishDto.getCategoryId()+"_"+dishDto.getStatus();
        redisTemplate.delete(dishReids);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(name!=null,Dish::getName,name);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo,dishLambdaQueryWrapper);
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record,dishDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            dishDtos.add(dishDto);
        }
        dishDtoPage.setRecords(dishDtos);
        return R.success(dishDtoPage);

    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto=dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        String dishReids="dish_"+dishDto.getCategoryId()+"_"+dishDto.getStatus();

        redisTemplate.delete(dishReids);
        return R.success("新增菜品成功");
    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtos=null;
        String dishReids="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        dishDtos= (List<DishDto>) redisTemplate.opsForValue().get(dishReids);
        if (dishDtos!=null){
            return R.success(dishDtos);
        }
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(dish.getName()),Dish::getName,dish.getName());
        queryWrapper.eq(null!=dish.getCategoryId(),Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishDto> arrayList = new ArrayList<>();
        for (Dish dish1 : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1,dishDto);
            Category category = categoryService.getById(dish1.getCategoryId());
            if(category!=null){
                dishDto.setCategoryName(category.getName());
            }
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,dish1.getId());
            dishDto.setFlavors(dishFlavorService.list(wrapper));
            arrayList.add(dishDto);
        }
        redisTemplate.opsForValue().set(dishReids,arrayList,5, TimeUnit.MINUTES);
        return R.success(arrayList);
    }

    @PostMapping("/status/1")
    @ApiOperation("启售状态")
    public R<String>  start(@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getId,id);
            Dish dish = dishService.getById(id);
            dish.setStatus(1);
            dishService.update(dish,queryWrapper);
        }
        return R.success("更改成功");
    }
    @PostMapping("/status/0")
    @ApiOperation("停止售状态")
    public R<String>  end(@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getId,id);
            Dish dish = dishService.getById(id);
            dish.setStatus(0);
            dishService.update(dish,queryWrapper);
        }
        return R.success("更改成功");
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public R<String>  delete(@RequestParam List<Long> ids){
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }





}
